using Joy.Erp.BasicArchives.Core.Filters;
using Joy.Erp.BasicArchives.HttpApi.SimplyClient.Core;
using Joy.Erp.BasicArchives.ItemMasters;
using System;
using System.Threading.Tasks;

namespace Joy.Erp.BasicArchives.HttpApi.SimplyClient.ItemMasters
{
    public class UnitGroupClientProxy
    {
        public readonly TokenClientProxy TokenClientProxy;

        public UnitGroupClientProxy(TokenClientProxy tokenClientProxy)
        {
            TokenClientProxy = tokenClientProxy;
        }

        public async Task<UnitGroupDto> GetAsync(Guid id, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/unit-group/{id}");

            return await ApiHandler.HttpGetAsync<UnitGroupDto>(token, url, logfunc);
        }

        public UnitGroupDto Get(Guid id, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await GetAsync(id, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<AuxiliaryUnitDto> GetChildAsync(Guid id, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/unit-group/child/{id}");

            return await ApiHandler.HttpGetAsync<AuxiliaryUnitDto>(token, url, logfunc);
        }

        public AuxiliaryUnitDto GetChild(Guid id, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await GetChildAsync(id, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<UnitGroupDto> FindAsync(string filter, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/unit-group/find?filter={filter}");

            return await ApiHandler.HttpGetAsync<UnitGroupDto>(token, url, logfunc);
        }

        public UnitGroupDto Find(string filter, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await FindAsync(filter, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<UnitGroupDto> FindBySourceKeyAsync(string sourceKey, string sourceType, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/unit-group/find-by-source-key?sourceKey={sourceKey}&sourceType={sourceType}");

            return await ApiHandler.HttpGetAsync<UnitGroupDto>(token, url, logfunc);
        }

        public UnitGroupDto FindBySourceKey(string sourceKey, string sourceType, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await FindBySourceKeyAsync(sourceKey, sourceType, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<PagedResultDto<UnitGroupDto>> GetListAsync(FilterRequestDto input, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/unit-group?filter={input.Filter}&sorting={input.Sorting}&skipCount={input.SkipCount}&maxResultCount={input.MaxResultCount}");

            return await ApiHandler.HttpGetAsync<PagedResultDto<UnitGroupDto>>(token, url, logfunc);
        }

        public PagedResultDto<UnitGroupDto> GetList(FilterRequestDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await GetListAsync(input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<PagedResultDto<UnitGroupDto>> QueryAsync(FilterGroupRequestDto input, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/unit-group/query");

            return await ApiHandler.HttpPostAsync<PagedResultDto<UnitGroupDto>>(token, url, input, logfunc);
        }

        public PagedResultDto<UnitGroupDto> Query(FilterGroupRequestDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await QueryAsync(input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<PagedResultDto<AuxiliaryUnitDto>> QueryChildrenAsync(FilterGroupRequestDto input, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/unit-group/query-children");

            return await ApiHandler.HttpPostAsync<PagedResultDto<AuxiliaryUnitDto>>(token, url, input, logfunc);
        }

        public PagedResultDto<AuxiliaryUnitDto> QueryChildren(FilterGroupRequestDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await QueryChildrenAsync(input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<UnitGroupDto> CreateAsync(UnitGroupCreateDto input, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/unit-group");

            return await ApiHandler.HttpPostAsync<UnitGroupDto>(token, url, input, logfunc);
        }

        public UnitGroupDto Create(UnitGroupCreateDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await CreateAsync(input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<UnitGroupDto> UpdateAsync(Guid id, UnitGroupUpdateDto input, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/unit-group/{id}");

            return await ApiHandler.HttpPutAsync<UnitGroupDto>(token, url, input, logfunc);
        }

        public UnitGroupDto Update(Guid id, UnitGroupUpdateDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await UpdateAsync(id, input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task DeleteAsync(Guid id, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/unit-group/{id}");

            await ApiHandler.HttpDeleteAsync(token, url, logfunc);
        }

        public void Delete(Guid id, Action<string> logfunc = null)
        {
            try
            {
                Task.Run(async () => await DeleteAsync(id, logfunc));
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }
    }
} 
